package com.group.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.view.BaseResult;
import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.group.model.*;
import com.group.util.NoteException;

/**
 * 用户关注等
 * 
 * @author zhaolei
 *
 */
@Controller
public class UserInfoController extends BaseController {

	/**
	 * 根据id查询用户
	 * 
	 * @param userId
	 * @return
	 */
	@GetMapping("/user/id")
	@ResponseBody
	public GeneralResult<User> getUser(Long userId) {
		User user = userService.getUser(userId);

		return new GeneralResult<User>().setValue(user);
	}

	/**
	 * 查询用户是否有红包
	 * 
	 * @param request
	 * @return
	 */
	@GetMapping("/user/hasRedpack")
	@ResponseBody
	public synchronized GeneralResult<Boolean> hasRedpack(HttpServletRequest request) {
		User user = getUser(request);

		UserTicker ut = tickerService.getUserTicker(user.getId(), "czr");

		// if (ut == null || ut.getNum() == 0d) {
		// tickerService.addTicker(user.getId(), "czr", 30d, "系统赠送");
		//
		// return new GeneralResult<Boolean>().setValue(true);
		// }
		return new GeneralResult<Boolean>().setValue(false);
	}

	/**
	 * 查询用户钱包
	 * 
	 * @param userId
	 * @param cid
	 * @return
	 */
	@GetMapping("/user/ticker/query")
	@ResponseBody
	public ListResult<UserTicker> queryUserTicker(Long userId, String cid) {
		List<UserTicker> uts = userDao.queryUserTicker(userId, cid);

		// 补充币的信息
		if (uts != null) {
			for (UserTicker ut : uts) {
				ut.setTicker(beanDao.quickQueryOne(Ticker.class, "cid", ut.getCid()));
			}
		}

		return new ListResult<UserTicker>().setList(uts);
	}

	/**
	 * 查询钱包日志
	 * @param request
	 * @param cid
	 * @param reason
	 * @param start
	 * @param limit
	 * @return
	 */
	@GetMapping("/user/ticker/log/query")
	@ResponseBody
	public ListResult<UserTickerLog> queryUserTickerLog(HttpServletRequest request, String cid, String reason, Integer start, Integer limit) {
		Long userId = getUser(request).getId();
		if (cid == null) {
			throw new NoteException("参数错误");
		}

		return new ListResult<UserTickerLog>().setList(tickerDao.queryUserTickerLog(userId, cid, reason, start, limit)).setTotal(tickerDao.queryUserTickerLogCount(userId, cid, reason));
	}

	/**
	 * 查询钱包日志
	 *
	 * @param userId
	 * @param cid
	 * @param reason
	 * @param start
	 * @param limit
	 * @return
	 */
	@GetMapping("/user/ticker/log/query/web")
	@ResponseBody
	public ListResult<UserTickerLog> queryUserTickerLog(Long userId, String cid, String reason, Integer start, Integer limit) {
		if (cid == null) {
			throw new NoteException("参数错误");
		}

		return new ListResult<UserTickerLog>().setList(tickerDao.queryUserTickerLog(userId, cid, reason, start, limit)).setTotal(tickerDao.queryUserTickerLogCount(userId, cid, reason));
	}

	/**
	 * 关注其他用户
	 * 
	 * @param userId
	 *            当前用户
	 * @param followUserId
	 *            要关注的用户id
	 * @return
	 */
	@GetMapping("/user/follow")
	@ResponseBody
	public BaseResult followUser(Long userId, Long followUserId) {
		if (userId == null || followUserId == null) {
			throw new NoteException("参数错误");
		}

		UserFollow follow = new UserFollow();
		follow.setUserId(userId);
		follow.setFollowUserId(followUserId);

		if (beanDao.queryOne(follow) == null) {
			beanDao.save(follow);
			// 更新消息
			messageService.addFans(userId, followUserId);

			// 更新用户关注的人数
			userDao.updateUserFollow(userId);

			// 更新用户的粉丝数
			userDao.updateUserFans(followUserId);
		}

		return new BaseResult();
	}

	/**
	 * 取消关注
	 * 
	 * @param userId
	 * @param followUserId
	 * @return
	 */
	@GetMapping("/user/unfollow")
	@ResponseBody
	public BaseResult unfollowUser(Long userId, Long followUserId) {
		if (userId == null || followUserId == null) {
			throw new NoteException("参数错误");
		}

		UserFollow follow = new UserFollow();
		follow.setUserId(userId);
		follow.setFollowUserId(followUserId);

		UserFollow exist = beanDao.queryOne(follow);
		if (exist != null) {
			beanDao.quickDelete(UserFollow.class, "id", exist.getId());

			// 更新用户关注的人数
			userDao.updateUserFollow(userId);

			// 更新用户的粉丝数
			userDao.updateUserFans(followUserId);
		}

		return new BaseResult();
	}

	/**
	 * 查询用户关注的人列表
	 * 
	 * @param userId
	 * @param loginUserId
	 *            当前登录用户,传此字段返回包含是否关注字段
	 * @param start
	 * @param limit
	 * @return
	 */
	@GetMapping("/user/follow/query")
	@ResponseBody
	public ListResult<User> queryUserFollow(Long userId, Long loginUserId, Integer start, Integer limit) {
		List<User> users = userDao.queryUserFollow(userId, start, limit);
		userService.fillUser(users, loginUserId);

		return new ListResult<User>().setList(users).setTotal(userDao.queryUserFollowCount(userId));
	}

	/**
	 * 查询用户粉丝列表
	 * 
	 * @param userId
	 * @param loginUserId
	 *            当前登录用户,传此字段返回包含是否关注字段
	 * @param start
	 * @param limit
	 * @return
	 */
	@GetMapping("/user/fans/query")
	@ResponseBody
	public ListResult<User> queryUserFans(Long userId, Long loginUserId, Integer start, Integer limit) {
		List<User> users = userDao.queryUserFans(userId, start, limit);
		userService.fillUser(users, loginUserId);

		return new ListResult<User>().setList(users).setTotal(userDao.queryUserFollowFansCount(userId));
	}

	/**
	 * 查询当前userId是否关注followUserId
	 * 
	 * @param userId
	 * @param followUserId
	 * @return
	 */
	@GetMapping("/user/isfollow")
	@ResponseBody
	public GeneralResult<Boolean> queryIsfollow(Long userId, Long followUserId) {
		boolean flag = false;

		UserFollow para = new UserFollow();
		para.setUserId(userId);
		para.setFollowUserId(followUserId);
		if (beanDao.queryOne(para) != null) {
			flag = true;
		}

		return new GeneralResult<Boolean>().setValue(flag);
	}

}
